package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.qo.EmployeeQueryObject;
import cn.wolfcode.rbac.qo.JSONResult;
import cn.wolfcode.rbac.qo.PageResult;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@RequestMapping("/employee")
@Controller
/**
 * Demo class
 *
 * @author keriezhang
 * @date 2016/10/31
 */
public class EmployeeController {

	@Autowired
	private IEmployeeService employeeService;
	@Autowired
	private IDepartmentService departmentService;
	@Autowired
	private IRoleService roleService;
	@RequiredPermission(name = "员工列表",expression = "employee:list")
	@RequestMapping("/list")
	public ModelAndView list(EmployeeQueryObject qo) {
		ModelAndView mv = new ModelAndView();
		List<Employee> employees = employeeService.selectList(qo);
		mv.addObject("employees", employees);
		List<Department> departments = departmentService.selectList();
		mv.addObject("departments",departments);
		mv.addObject("qo", qo);
		mv.setViewName("/employee/list");
		return mv;
	}
	@RequiredPermission(name = "员工编辑",expression = "employee:input")
	@RequestMapping("/input")
	public ModelAndView input(Long id){
		ModelAndView mv=new ModelAndView();
		if(id!=null){
			mv.addObject("employee",employeeService.selectOne(id));
			mv.addObject("empRoleList",employeeService.seleteRolesByEmployeeId(id));
		}



		mv.addObject("roleList",roleService.listAll());
		mv.addObject("departments",departmentService.selectList());

		mv.setViewName("/employee/input");
		return mv;
	}
	@RequiredPermission(name = "员工保存/更新",expression = "employee:saveOrUpdate")
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(Employee employee,Long[]ids){
		//保存
		if(employee.getId()==null)
		{
			employeeService.insertEmployee(employee);
			Long id=employeeService.selectByName(employee.getName(),employee.getPassword()).getId();
			employeeService.insertRelationRoleIdAndEmployeeId(id,ids);

		}else {
			employeeService.UpdateByEmployeeId(employee);

			employeeService.insertRelationRoleIdAndEmployeeId(employee.getId(), ids);
		}
		return "redirect:/employee/list";




	}
	@RequiredPermission(name = "员工删除",expression = "employee:delete")
	@RequestMapping("/delete")
	public String delete(Long id){

		employeeService.deleteById(id);
		return "redirect:/employee/list";
	}

	@RequestMapping("/batchDeleteByIds")
	@ResponseBody
	public JSONResult batchDeleteByIds(Long[] ids){
		for (Long id:ids
			 ) {
			employeeService.deleteById(id);
		}
		return new JSONResult(true, "success");
	}


}
